function t(e, a) {
    s.showLoading("正在提交数据");
    var i = s.getUTCDateInSeconds(e.data.date), n = s.getTimeInSeconds(e.data.time), o = a.detail.value.token || "", r = a.detail.value.tokenDesc || "", l = e.data.verifyCode;
    l = l ? parseInt(l) : "";
    var u = 0;
    "normal" == e.data.lottoType ? u = 0 : "mini" == e.data.lottoType ? u = 1 : "game" == e.data.lottoType && (u = 2, 
    m = a.detail.value.rule);
    var h = e.data.phone, c = e.data.oldPhone, m = e.data.rule, p = e.data.title;
    h = c || h, "mini" == e.data.lottoType && (m = a.detail.value.rule, p = e.data.prizes[0].name);
    var g = {
        title: p,
        content: m,
        mode: u,
        type: parseInt(e.data.typeIndex),
        prizes: e.data.prizes,
        banners: e.data.banners,
        sign_name: e.data.signName,
        wx_no: e.data.wxNo,
        no_forward: e.data.forwardIndex ? 1 : 0,
        req_token: o.trim(),
        token_desc: r.trim(),
        group_qr: e.data.qrcode || "",
        phone: h,
        code: l,
        access_token: wx.getStorageSync("accessToken")
    };
    if (0 == e.data.typeIndex ? g.date = i + n : 1 == e.data.typeIndex ? g.limit = parseInt(e.data.joinNum) : 2 == e.data.typeIndex && (g.limit = 0), 
    "game" == e.data.lottoType) {
        g.start_date = s.getUTCDateInSeconds(e.data.dateStart) + s.getTimeInSeconds(e.data.timeStart), 
        g.end_date = s.getUTCDateInSeconds(e.data.dateEnd) + s.getTimeInSeconds(e.data.timeEnd);
        for (var f = e.data.prizes, y = 0; y < f.length; y++) f[y].chance = 100 * parseFloat(f[y].chance);
        g.prizes = f, g.hide_winner = e.data.lottoEnabled;
    }
    wx.request({
        url: d.globalData.host + "/lotto/v1/create",
        data: g,
        method: "POST",
        success: function(i) {
            e.submitFlag = !1, s.hideLoading(), -500 != i.data.sta ? 0 == i.data.sta ? i.data.data && wx.redirectTo({
                url: "../detail/detail?lid=" + i.data.data.lid
            }) : s.showFailedToast("创建失败，请重试", i.data.msg) : s.login(function() {
                t(e, a);
            });
        },
        fail: function(t) {
            e.submitFlag = !1, s.showFailedToast("创建失败，请重试");
        }
    });
}

function e(t) {
    for (var e = 0, a = t.length, i = 0; i < a; i++) {
        if (s.isTextEmpty(t[i].name)) return 1;
        s.isTextEmpty(t[i].img) && e++;
    }
    return 0 != e ? 2 : 0;
}

function a(t) {
    null == t.data.lotto && s.showLoading("正在加载数据"), wx.request({
        url: d.globalData.host + "/lotto/v1/detail",
        data: {
            lid: t.data.lid,
            access_token: s.getToken()
        },
        method: "GET",
        dataType: "json",
        success: function(e) {
            if (s.hideLoading(), -500 != e.data.sta) if (0 == e.data.sta) {
                var i = e.data.data;
                if (i.date) i.time = s.formatDate(1e3 * i.date, "HH:mm"), i.date = s.formatDate(1e3 * i.date, "yyyy-MM-dd"); else {
                    var n = s.getDaysLater(3), o = s.formatDate(n, "yyyy-MM-dd");
                    i.date = o, i.time = "10:30";
                }
                if (i.start_date) i.timeStart = s.formatDate(1e3 * i.start_date, "HH:mm"), i.dateStart = s.formatDate(1e3 * i.start_date, "yyyy-MM-dd"); else {
                    var d = s.formatDate(s.getDaysLater(0), "yyyy-MM-dd");
                    i.dateStart = d, i.timeStart = "10:30";
                }
                if (i.end_date) i.timeEnd = s.formatDate(1e3 * i.end_date, "HH:mm"), i.dateEnd = s.formatDate(1e3 * i.end_date, "yyyy-MM-dd"); else {
                    var r = s.formatDate(s.getDaysLater(3), "yyyy-MM-dd");
                    i.dateEnd = r, i.timeEnd = "10:30";
                }
                var l = i.prizes;
                if (2 == i.mode) {
                    for (var u = l, h = 0; h < u.length; h++) u[h].chance = parseFloat(u[h].chance) / 100;
                    l = u;
                }
                var c = i.content;
                "string" == typeof c && (c = "" == c ? [] : [ {
                    type: "text",
                    value: c,
                    text: ""
                } ]), t.setData({
                    title: i.title,
                    titleDefault: i.title,
                    rule: c,
                    ruleDefault: i.content,
                    titleEmpty: !i.title,
                    ruleEmpty: !i.content,
                    date: i.date,
                    time: i.time,
                    dateStart: i.dateStart,
                    timeStart: i.timeStart,
                    dateEnd: i.dateEnd,
                    timeEnd: i.timeEnd,
                    banners: i.banners,
                    joinNum: i.limit,
                    prizes: l,
                    prizesDefalut: l,
                    typeIndex: parseInt(i.type),
                    signName: i.owner_name,
                    wxNo: i.owner_wx,
                    defaultWxNo: i.owner_wx
                });
            } else -1 == e.data.sta && "data not found" == e.data.msg ? t.setData({
                dataNotFound: !0
            }) : s.showFailedToast("数据加载失败", e.data.msg); else s.login(function() {
                a(t), t.setData({
                    hasUserInfo: !0
                });
            }, function() {
                t.setData({
                    hasUserInfo: !1
                });
            });
        },
        fail: function(e) {
            s.hideLoading(), null == t.data.enroll && t.setData({
                showLoadError: 1,
                isStartLoad: !1
            });
        }
    });
}

function i(t) {
    s.showLoading("正在获取验证码..."), wx.request({
        url: d.globalData.host + "/lotto/v1/code",
        data: {
            phone: t.data.phone,
            access_token: wx.getStorageSync("accessToken")
        },
        method: "POST",
        success: function(e) {
            s.hideLoading(), -500 != e.data.sta ? 0 == e.data.sta ? n(t) : s.showFailedToast("获取验证码失败，请重试", e.data.msg) : s.login(function() {
                i(t);
            });
        },
        fail: function(t) {
            s.hideLoading(), s.showFailedToast("获取验证码失败，请重试");
        }
    });
}

function n(t) {
    var e = t.data.verifyCodeCountdownNum - 1;
    e > 0 ? (t.setData({
        verifyCodeCountdownNum: e,
        verifyCodeText: e + "后重新获取"
    }), setTimeout(function() {
        n(t);
    }, 1e3)) : t.setData({
        verifyCodeCountdownNum: 60,
        verifyCodeText: "获取验证码"
    });
}

var s = require("../../utils/util.js"), o = require("../../tmp/tmp.js"), d = getApp();

Page({
    data: {
        title: "",
        lottoType: "normal",
        rule: "",
        titleEmpty: !0,
        ruleEmpty: !0,
        date: "",
        time: "10:30",
        timeStart: "10:30",
        timeEnd: "10:30",
        type: [ "按时间自动开奖", "按人数自动开奖", "手动开奖" ],
        typeIndex: 0,
        forward: [ "所有人可分享", "仅创建者可分享" ],
        forwardIndex: 0,
        prizes: [ {
            name: "",
            count: 1,
            chance: "0.00",
            img: ""
        } ],
        prizesDefalut: [ {
            name: "",
            count: 1,
            chance: "0.00",
            img: ""
        } ],
        joinNum: "",
        banners: [],
        signName: s.getStorage("userInfo").nickName,
        wxNo: "",
        showMore: !0,
        token: "",
        tokenDesc: "",
        tokenEmpty: !0,
        tokenDescEmpty: !0,
        tokenEnabled: !1,
        createTips: !1,
        verifyCode: "",
        verifyCodeEmpty: !0,
        verifyCodeCountdownNum: 60,
        verifyCodeText: "获取验证码",
        phone: "",
        phoneEmpty: !0,
        oldPhone: s.getStorage("userInfo").phone || "",
        huomaDialog: !1,
        huoma: d.globalData.huoma,
        phoneFlag: !1,
        authPhone: "12345678",
        chanceSum: 100,
        lottoEnabled: 1
    },
    onLoad: function(t) {
        if ("mini" == t.type ? this.setData({
            lottoType: "mini"
        }) : "game" == t.type && this.setData({
            lottoType: "game"
        }), t.lid) this.setData({
            lid: t.lid
        }), a(this); else {
            var e = new Date(), i = s.getDaysLater(3), n = s.getDaysLater(0), d = s.getDaysLater(3), r = new Date();
            r.setYear(r.getFullYear() + 5);
            var l = s.formatDate(i, "yyyy-MM-dd"), u = s.formatDate(n, "yyyy-MM-dd"), h = s.formatDate(d, "yyyy-MM-dd");
            this.setData({
                date: l,
                dateStart: u,
                dateEnd: h,
                minDate: s.formatDate(e, "yyyy-MM-dd"),
                maxDate: s.formatDate(r, "yyyy-MM-dd"),
                diffTime: s.getDiffTime(l, this.data.time),
                prizesDefalut: this.data.prizes
            });
            var c = s.getStorage("lotto_data");
            if (c && this.data.lottoType == c.lottoType) {
                var m = c.prizes;
                this.setData({
                    title: c.title,
                    titleDefault: c.title,
                    rule: c.rule,
                    ruleDefault: c.rule,
                    titleEmpty: c.titleEmpty,
                    ruleEmpty: c.ruleEmpty,
                    date: c.date < l ? l : c.date,
                    time: c.time,
                    dateStart: c.dateStart,
                    timeStart: c.timeStart,
                    dateEnd: c.dateEnd,
                    timeEnd: c.timeEnd,
                    banners: c.banners,
                    joinNum: c.joinNum,
                    prizes: m,
                    prizesDefalut: m,
                    typeIndex: parseInt(c.typeIndex),
                    signName: c.signName,
                    wxNo: c.wxNo
                });
            }
            wx.removeStorageSync("lotto_data");
        }
        o.huoma(this);
    },
    onShow: function() {
        if (this.data.readCatch) {
            var t = s.getStorage("rules") || [];
            this.setData({
                rule: t
            });
        }
        this.setData({
            oldPhone: s.getStorage("userInfo").phone || ""
        });
    },
    onUnload: function() {
        1 == wx.getStorageSync("create_lotto") ? (wx.removeStorageSync("create_lotto"), 
        wx.removeStorageSync("lotto_data"), wx.removeStorageSync("lid")) : wx.setStorageSync("lotto_data", this.data);
    },
    agree: function(t) {
        this.setData({
            createTips: !1
        });
    },
    inputTitle: function(t) {
        this.setData({
            title: t.detail.value,
            titleEmpty: 0 == t.detail.value.length
        });
    },
    inputRlues: function(t) {
        this.setData({
            rule: [ {
                type: "text",
                value: t.detail.value
            } ]
        });
    },
    clearTitle: function() {
        this.setData({
            title: "",
            titleDefault: "",
            titleEmpty: !0
        });
    },
    inputRule: function(t) {
        this.setData({
            rule: t.detail.value,
            ruleEmpty: 0 == t.detail.value.length
        });
    },
    clearRule: function() {
        this.setData({
            rule: "",
            ruleDefault: "",
            ruleEmpty: !0
        });
    },
    clearJoinNum: function() {
        this.setData({
            joinNum: "",
            joinNumEmpty: !0
        });
    },
    inputSignName: function(t) {
        this.setData({
            signName: t.detail.value
        });
    },
    clearName: function() {
        this.setData({
            signName: ""
        });
    },
    inputWxno: function(t) {
        this.setData({
            wxNo: t.detail.value.trim()
        });
    },
    clearWxno: function() {
        this.setData({
            wxNo: "",
            defaultWxNo: ""
        });
    },
    inputPhone: function(t) {
        var e = t.detail.value;
        this.setData({
            phone: e,
            noAuthPhone: 0 != e.length,
            showVerifyCode: e.length > 0 && e != this.data.originPhone
        });
    },
    clearPhone: function() {
        this.setData({
            phone: "",
            defaultPhone: "",
            noAuthPhone: !1,
            showVerifyCode: !1
        });
    },
    getPhoneNumber: function(t) {
        var e = this;
        "getPhoneNumber:ok" == t.detail.errMsg ? (s.showLoading("正在获取手机号码..."), wx.request({
            url: d.globalData.host + "/common/decrypt",
            data: {
                encrypted_data: t.detail.encryptedData || "",
                iv: t.detail.iv || "",
                access_token: wx.getStorageSync("accessToken")
            },
            method: "POST",
            success: function(t) {
                s.hideLoading(), 0 == t.data.sta ? e.setData({
                    phoneFlag: !0,
                    defaultPhone: t.data.data.phoneNumber || "",
                    phone: t.data.data.phoneNumber || "",
                    authPhone: t.data.data.phoneNumber || ""
                }) : (e.setData({
                    phoneFlag: !0,
                    authPhone: ""
                }), s.showFailedToast("获取手机号码失败，请重试", t.data.msg));
            },
            fail: function(t) {
                e.setData({
                    phoneFlag: !0,
                    authPhone: ""
                }), s.hideLoading(), s.showFailedToast("获取手机号码失败，请重试");
            }
        })) : this.setData({
            phoneFlag: !0,
            authPhone: ""
        });
    },
    changeDate: function(t) {
        var e = t.detail.value;
        this.setData({
            date: e,
            diffTime: s.getDiffTime(e, this.data.time)
        });
    },
    changeTime: function(t) {
        var e = t.detail.value;
        this.setData({
            time: e,
            diffTime: s.getDiffTime(this.data.date, e)
        });
    },
    changeDateStart: function(t) {
        var e = t.detail.value;
        this.setData({
            dateStart: e
        });
    },
    changeTimeStart: function(t) {
        var e = t.detail.value;
        this.setData({
            timeStart: e
        });
    },
    changeDateEnd: function(t) {
        var e = t.detail.value;
        this.setData({
            dateEnd: e
        });
    },
    changeTimeEnd: function(t) {
        var e = t.detail.value;
        this.setData({
            timeEnd: e
        });
    },
    changeType: function(t) {
        this.setData({
            typeIndex: t.detail.value
        });
    },
    addPrize: function(t) {
        var e = this.data.prizes;
        "game" == this.data.lottoType && e.length > 6 ? s.showModelTips("最多允许创建7个选项") : e.length > 9 ? s.showModelTips("最多允许创建10个选项") : (e.push({
            name: "",
            count: 1,
            img: "",
            chance: "0.00"
        }), this.setData({
            prizes: e
        }));
    },
    deletePrize: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.prizes;
        if (a.length < 2) s.showModelTips("最少必须有一个奖品"); else {
            a.splice(e, 1);
            for (var i = 0; i < a.length; i++) a[i].chance;
            this.setData({
                prizes: a
            });
        }
    },
    addPic: function(t) {
        var e = t.currentTarget.dataset.type, a = this, i = 1, n = 0;
        if (2 == e) {
            var o = a.data.banners;
            if (i = 6 - o.length, o.length >= 6) return void s.showModelTips("最多只能上传\b\b\b六张图片");
        } else n = t.currentTarget.dataset.index;
        this.setData({
            readCatch: !1
        }), wx.chooseImage({
            count: i,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var i = t.tempFilePaths;
                a.setData({
                    tempPaths: i,
                    tempIndex: 0
                }), s.showLoading("上传中..."), a.uploadImages(i, e, n);
            }
        });
    },
    uploadImages: function(t, e, a) {
        var i = t[0];
        2 == e && (i = t[a]);
        var n = this;
        wx.uploadFile({
            url: d.globalData.host + "/file/v1/upload",
            filePath: i,
            name: "file",
            formData: {
                access_token: s.getStorage("accessToken"),
                type: "qrcode" == e ? "qrcode" : ""
            },
            success: function(i) {
                console.log("uploadFile ===== "), console.log(i);
                var o = i.data, d = JSON.parse(o);
                if (0 == d.sta) {
                    var r = d.data.urls;
                    if (2 == e) {
                        var l = n.data.banners;
                        l.push(r[0]), n.setData({
                            banners: l
                        }), t.length - 1 > a ? n.uploadImages(t, e, a + 1) : s.hideLoading();
                    } else if ("qrcode" == e) s.hideLoading(), n.setData({
                        qrcode: r[0]
                    }); else {
                        s.hideLoading();
                        var u = n.data.prizes;
                        u[a].img = r[0], n.setData({
                            prizes: u
                        });
                    }
                } else s.hideLoading(), s.showFailedToast(d.msg);
            },
            fail: function(t) {
                s.hideLoading(), s.showFailedToast("上传图片失败，请重试");
            }
        });
    },
    deletePic: function(t) {
        var e = t.currentTarget.dataset.type, a = t.currentTarget.dataset.index;
        if (1 == e) {
            var i = this.data.prizes;
            i[a].img = "", this.setData({
                prizes: i
            });
        } else if ("qrcode" == e) this.setData({
            qrcode: ""
        }); else {
            var n = this.data.banners;
            n.splice(a, 1), this.setData({
                banners: n
            });
        }
    },
    showPic: function(t) {
        var e = t.currentTarget.dataset.type, a = t.currentTarget.dataset.url, i = [ a ];
        2 == e && (i = this.data.banners), wx.previewImage({
            current: a,
            urls: i
        });
    },
    inputName: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.prizes;
        a[e].name = t.detail.value, this.setData({
            prizes: a
        });
    },
    inputCount: function(t) {
        var e = t.currentTarget.dataset.index;
        this.setPrizesCount(e, t.detail.value);
    },
    inputChance: function(t) {
        var e = t.currentTarget.dataset.index;
        this.setPrizesChance(e, t.detail.value);
    },
    inputBlur: function(t) {
        var e = t.currentTarget.dataset.index, a = t.detail.value;
        a = "" == a ? 0 : a, this.setPrizesCount(e, a);
    },
    inputBlurChance: function(t) {
        var e = t.currentTarget.dataset.index, a = t.detail.value;
        a = "" == a ? 0 : a, this.setPrizesChance(e, a);
    },
    huomaDialog: function() {
        this.setData({
            huomaDialog: !0
        });
    },
    setPrizesCount: function(t, e) {
        var a = this.data.prizes;
        a[t].count = parseInt(e), a[t].count < 1 && (a[t].count = 1), this.setData({
            prizes: a
        });
    },
    setPrizesChance: function(t, e) {
        if (e >= 100 || e < 0) return this.setData({
            prizes: this.data.prizes
        }), void s.showModelTips("请输入正确的中奖概率");
        (a = this.data.prizes)[t].chance;
        return void (a[t].chance = parseFloat(e));
        var a;
    },
    minusPlus: function(t) {
        var e = t.currentTarget.dataset.index, a = 1;
        "minus" == t.currentTarget.dataset.type && (a = -1);
        var i = this.data.prizes;
        i[e].count = i[e].count + a, i[e].count > 999 && (i[e].count = 999), i[e].count < 1 && (i[e].count = 1), 
        this.setData({
            prizes: i
        });
    },
    previewPage: function() {
        s.setStorage("lotto_preview", this.data), wx.navigateTo({
            url: "/pages/preview/preview"
        });
    },
    touchstart: function(t) {
        var e = t.currentTarget.dataset.index, a = t.currentTarget.dataset.type, i = this.data.prizes;
        "minus" == a ? (i[e].pressMinus = !0, this.setData({
            prizes: i
        })) : (i[e].pressPlus = !0, this.setData({
            prizes: i
        }));
    },
    touchend: function() {
        for (var t = this.data.prizes, e = 0; e < t.length; e++) t[e].pressMinus = !1, t[e].pressPlus = !1;
        this.setData({
            prizes: t
        });
    },
    joinNum: function(t) {
        this.setData({
            joinNum: t.detail.value,
            joinNumEmpty: !t.detail.value.length
        });
    },
    submitFlag: !1,
    createLotto: function(a) {
        if (!this.submitFlag) {
            if (this.submitFlag = !0, setTimeout(function() {
                this.submitFlag = !1;
            }, 5e3), s.isTextEmpty(this.data.title) && "mini" != this.data.lottoType) return s.showModelTips("请输入抽奖活动标题"), 
            void (this.submitFlag = !1);
            var i = s.getUTCDateInSeconds(this.data.dateStart) + s.getTimeInSeconds(this.data.timeStart), n = s.getUTCDateInSeconds(this.data.dateEnd) + s.getTimeInSeconds(this.data.timeEnd), o = new Date().getTime();
            if ("game" == this.data.lottoType && i >= n) return s.showModelTips("抽奖开始时间不能大于结束时间"), 
            void (this.submitFlag = !1);
            if ("game" == this.data.lottoType && n <= parseInt(o / 1e3)) return s.showModelTips("结束时间不能小于当前时间"), 
            void (this.submitFlag = !1);
            if (1 == e(this.data.prizes)) return s.showModelTips("请将奖项标题填写完整"), void (this.submitFlag = !1);
            if (2 == e(this.data.prizes)) return s.showModelTips("您有未上传的奖项图片，请检查~"), void (this.submitFlag = !1);
            if (this.data.prizes.length < 1) return s.showModelTips("至少需要一个奖项"), void (this.submitFlag = !1);
            var d = this.data.joinNum;
            if ((0 == d || "" == d) && 1 == this.data.typeIndex) return s.showModelTips("请输入开奖人数"), 
            void (this.submitFlag = !1);
            if (0 == this.data.typeIndex && "game" != this.data.lottoType && new Date().getTime() >= 1e3 * (s.getUTCDateInSeconds(this.data.date) + s.getTimeInSeconds(this.data.time))) return s.showModelTips("开奖时间必须大于当前时间"), 
            void (this.submitFlag = !1);
            var r = !1, l = 0, u = this.data.prizes;
            if ("game" == this.data.lottoType) {
                for (i = 0; i < u.length; i++) 0 == parseFloat(u[i].chance) && (r = !0), l += 100 * parseFloat(u[i].chance);
                if (r) return s.showModelTips("奖项中奖概率不能为0"), void (this.submitFlag = !1);
                if (l > 1e4) return s.showModelTips("所有奖品中奖概率总和不能大于100"), void (this.submitFlag = !1);
            }
            var h = this.data.oldPhone, c = this.data.phone;
            if ("" == h && "" == c) return s.showModelTips("请输入手机号"), void (this.submitFlag = !1);
            if (this.data.authPhone.length > 7 && this.data.authPhone == this.data.phone && this.setData({
                verifyCode: 9999999
            }), "" == h && "" != c && "" == this.data.verifyCode && !(this.data.authPhone.length > 7 && this.data.authPhone == this.data.phone)) return s.showModelTips("请输入验证码"), 
            void (this.submitFlag = !1);
            "preview" == a.currentTarget.dataset.type ? this.previewPage() : (wx.setStorageSync("create_lotto", 1), 
            s.removeStorage("rules"), t(this, a));
        }
    },
    showMore: function() {
        var t = this.data.showMore;
        this.setData({
            showMore: !t
        });
    },
    inputToken: function(t) {
        this.setData({
            tokenEmpty: 0 == t.detail.value.length
        });
    },
    clearToken: function() {
        this.setData({
            token: "",
            tokenEmpty: !0
        });
    },
    inputTokenDesc: function(t) {
        this.setData({
            tokenDescEmpty: 0 == t.detail.value.length
        });
    },
    clearTokenDesc: function() {
        this.setData({
            tokenDesc: "",
            tokenDescEmpty: !0
        });
    },
    changeTokenEnabled: function(t) {
        this.setData({
            tokenEnabled: t.detail.value,
            token: "",
            tokenDesc: ""
        });
    },
    changeLottoEnabled: function(t) {
        this.setData({
            lottoEnabled: t.detail.value ? 0 : 1
        });
    },
    changeForward: function(t) {
        this.setData({
            forwardIndex: t.detail.value
        });
    },
    inputVerifyCode: function(t) {
        this.setData({
            verifyCode: t.detail.value
        });
    },
    clearVerifyCode: function() {
        this.setData({
            verifyCode: "",
            defaultVerifyCode: ""
        });
    },
    getVerifyCode: function(t) {
        var e = this.data.phone;
        s.isTextEmpty(e) ? s.showModelTips("请输入手机号") : (this.setData({
            phone: e
        }), i(this));
    },
    toRules: function() {
        s.buttonClicked(this), s.setStorage("rules", this.data.rule), this.setData({
            readCatch: !0
        }), wx.navigateTo({
            url: "/pages/rules/rules"
        });
    },
    toRulesText: function() {}
});